package com.karst.essentials.chapter06.galaxyExample
{
	import org.papervision3d.core.proto.CameraObject3D;
	
	internal class CameraController
	{	
		private var camera:CameraObject3D;
		private var player:Player ;
		private var axes:Array = ["x","y","z"];
		private var axis:String = axes[0];
		
		public function CameraController(camera3D:CameraObject3D,player3D:Player)
		{	
			camera = camera3D;
			player = player3D;
			
			createView();
		}
		
		private function createView() : void
		{
			camera.z = -1500;
			camera.y = 400;
			
			camera.focus = 20;
			camera.far = 20000;
			camera.target = player;
		}
		
		public function update():void
		{
			//zoom cam
			var cameraZoom:Number;
			 if( UserInputHandler.mouseDown )
			{
				cameraZoom =  200;
			}
			else
			{
				cameraZoom =  40;
			}
			camera.zoom -= (camera.zoom - cameraZoom) * 0.1;
			
			//cam mode
			 switch( UserInputHandler.camMode )
			{	
				case "thirdPerson":
					camera.copyTransform(player);
					camera.moveBackward(400);
					camera.moveUp(100);
					player.visible = true;
					break;
				 case "firstPerson":
					camera.copyTransform(player);
					camera.moveBackward(1);//to prevent lookAt error
					player.visible = false;
					break;
				case "randomCam":
					if(UserInputHandler.randomCamActive)
					{
						camera.x = player.x + (Math.random() * 1000 - 500);
						camera.y = player.y + (Math.random() * 1000 - 500);
						camera.z = player.z + (Math.random() * 1000 - 500);
						
						axis = axes[Math.floor(Math.random() * 3)] ;
						UserInputHandler.randomCamActive = false;
						player.visible = true;
					}
					else
					{
						camera[axis] += 2;
					}
					break;
				case "defaultCam":
					player.visible = true;
					camera.z = -1500;
					camera.y = 400;
					break; 
			} 
		}

	}
}